package com.medical.sys.web.controller;

import com.medical.common.JsonResult;
import com.medical.common.web.controller.BaseController;
import com.medical.sys.model.Department;
import com.medical.sys.service.DepartmentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by JUNZI on 2015/8/16.
 */
@Controller
@RequestMapping("/department")
//科室管理
public class DepartmentController extends BaseController {
    @Autowired
    private DepartmentService departmentService;


    //根据id返回department对象
    @ApiOperation(value="根据id返回department对象", notes="根据id返回department对象")
    @RequestMapping(value="/getone/{id}",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult getOne(@PathVariable String id){
        try {
            Department department = departmentService.selectByPrimaryKey(id);
            return new JsonResult("200", "", department);
        }catch (Exception e){
            return new JsonResult("500",e.getMessage());
        }
    }

    //返回Department的json数组
    @ApiOperation(value="返回Department的json数组", notes="返回Department的json数组")
    @RequestMapping(value = "",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult selectAllDepartment(){

        return new JsonResult( departmentService.selectAllDepartment());
    }

    /**
     *
     * @param name
     * @param description
     * @param type
     * @return
     */
    //新增acc_department表的信息
    @ApiOperation(value="新增acc_department表的信息", notes="新增acc_department表的信息")
    @RequestMapping(value="/post",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult save(@RequestParam(value = "name" ,required = false) String name,
                           @RequestParam(value = "description" ,required = false)String description,
                           @RequestParam(value = "type" ,required = false) String type){
        Department department=new Department();
        department.setName(name);
        department.setDescription(description);
        department.setType(type);
        try{
        	departmentService.saveDepartment(department);
        } catch(Exception e) {
        	return new JsonResult("500",e.getMessage());
        }
      
        return new JsonResult("200","",department);
    }

    //编辑
    @ApiOperation(value="编辑", notes="根据id编辑department对象")
    @RequestMapping(value="/patch/{id}",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult update(@PathVariable String id,@RequestParam("name") String name,
                             @RequestParam("type")String type,@RequestParam("description")String description){

        try{
            Department department=departmentService.selectByPrimaryKey(id);
            department.setName(name);
            department.setType(type);
            department.setDescription(description);
            departmentService.updateByPrimaryKey(department);
            return new JsonResult("200","",department);
        } catch(Exception e) {
            return new JsonResult("500",e.getMessage());
        }
    }

    //删除
    @ApiOperation(value="删除", notes="根据id删除department对象")
    @RequestMapping(value="/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delete(@PathVariable String id){
        
        try{
    	    departmentService.deleteDepartment(id);
            return new JsonResult("200","删除成功");

        } catch(Exception e) {
            return new JsonResult("500",e.getMessage());
        }
    }
}
